package com.neuedu.controller;

import com.neuedu.common.ServerResponse;
import com.neuedu.entity.CheckRequest;
import com.neuedu.entity.Doctor;
import com.neuedu.service.CheckRequestService;
import com.neuedu.util.StringUtil;
import com.neuedu.vo.CheckRequestCondition;
import com.neuedu.vo.CheckRequestVO;
import com.neuedu.vo.CheckResultVO;
import com.neuedu.vo.CheckVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Arrays;
import java.util.List;

@Api("检查申请模块")
@RestController
@RequestMapping("/request")
public class CheckRequestController {

    @Autowired
    private CheckRequestService checkRequestService;

    @ApiOperation("开具检查申请")
    @PostMapping("/add")
    public ServerResponse add(String registerId, String checkIds) {

        //调用业务方法
        if(checkRequestService.addCheckRequest(registerId, checkIds)){
            return ServerResponse.success("开具检查成功");
        }else{
            return ServerResponse.success("开具检查失败");
        }
    }

    @ApiOperation("获取检查项目列表---收费时使用")
    @GetMapping("/list/{registerId}")
    public ServerResponse list(@PathVariable String registerId) {

        //调用业务方法
        List<CheckVO> checkVOList = checkRequestService.getCheckList(registerId);

        return ServerResponse.success("").data("checkVOList", checkVOList);
    }

    @ApiOperation("检查缴费")
    @PutMapping("/fee/{registerId}")
    public ServerResponse fee(@PathVariable String registerId) {

        //调用业务方法
        if(checkRequestService.feeCheckList(registerId)){
            return ServerResponse.success("检查缴费成功");
        }else{
            return ServerResponse.error("检查缴费失败");
        }
    }

    @ApiOperation("获取检查项目列表---执行检查时使用")
    @GetMapping("/list")
    public ServerResponse list(CheckRequestCondition checkRequestCondition, HttpSession session) {

        System.out.println(checkRequestCondition);

        //从session中获取当前登录的用户对象
        Doctor doctor = (Doctor)session.getAttribute("currentUser");

        System.out.println(doctor == null);
        //填充医生编号到查询条件对象中
        checkRequestCondition.setDoctorId(doctor.getDoctorId());

        System.out.println(checkRequestCondition);
        //调用业务方法
        List<CheckRequestVO> checkRequestVOList = checkRequestService.getCheckList(checkRequestCondition);
        System.out.println(checkRequestVOList);

        return ServerResponse.success("").data("checkRequestVOList", checkRequestVOList);
    }

    @ApiOperation("上传检查结果")
    @PostMapping("/upload")
    public ServerResponse upload(CheckRequest checkRequest, MultipartFile[] files, HttpSession session){

        System.out.println("原始的checkRequest=" + checkRequest);
        System.out.println(Arrays.toString(files));

        //从session属性范围中获取当前登录的医生编号
        Doctor doctor = (Doctor)session.getAttribute("currentUser");

        //填充医生编号
        checkRequest.setDoctorId(doctor.getDoctorId());
        System.out.println("新的checkRequest=" + checkRequest);
        //获取服务器上的文件保存路径
        String serverFilePath = Thread.currentThread().getContextClassLoader().getResource("static/image/check").getPath();

        System.out.println(serverFilePath);
        //多个检查图片的文件名拼接
        String checkPhoto = "";

        //遍历每一个上传的文件
        for (MultipartFile file : files) {

            //获取原始的文件名
            String originalFilename = file.getOriginalFilename();

            //获取文件的扩展名
            String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

            //文件改名
            String serverFilename = StringUtil.generateFilename() + "." + extension;
            System.out.println(serverFilename);

            //创建目标文件对象
            File dest = new File(serverFilePath + "/" + serverFilename);

            try {

                //文件数据传输
                file.transferTo(dest);

                //设置photo为改名后的文件名
                checkPhoto += serverFilename + "#";

            } catch (Exception e) {
                return ServerResponse.error("检查结果图片上传失败");
            }
        }

        //去掉最后一个分隔符
        if(checkPhoto.length() > 0){
            checkPhoto = checkPhoto.substring(0, checkPhoto.length() - 1);
        }

        //设置上传照片的文件名列表
        checkRequest.setCheckPhoto(checkPhoto);

        System.out.println("上传文件后的checkRequest=" + checkRequest);

        if(checkRequestService.modifyCheckResult(checkRequest)){
            return ServerResponse.success("上传检查结果成功");
        }else{
            return ServerResponse.error("上传检查结果失败");
        }

    }

    @ApiOperation("查看检查结果")
    @GetMapping("/view/{registerId}")
    public ServerResponse view(@PathVariable String registerId){
        List<CheckResultVO> checkResultVOList = checkRequestService.getCheckResult(registerId);
        return ServerResponse.success("").data("checkResultVOList", checkResultVOList);
    }
}